<?php

/*
 * Table/XTable.php
 */

namespace Ticket\Model\Table;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Ticket\Model\Object\XObject;

class XTable extends AbstractTableGateway {

    protected $table = '';

    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     * @param string $table
     * @param mixed $prototype
     * @throws \Exception
     */
    public function __construct(Adapter $adapter, $table, $prototype = null) {
        if (!is_string($table)) {
            $typeOf = get_class($table);
            throw new \Exception("Le nom de la table envoyé en paramètre n'est pas une chaine de caractère. C'est un \"$typeOf\"");
        }
        $this->adapter = $adapter;
        $this->table   = $table;
        $prototype     = (is_null($prototype)) ? new XObject() : $prototype;
        $this->initialisation();
    }

    /**
     * Initialisation de la classe
     * 
     * @param mixed $prototype
     */
    protected function initialisation($prototype) {
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype($prototype);
    }

    /**
     * Tous les enregistrement
     * 
     * @param string $order
     * @return ResultSet
     */
    public function getAll($order = 'id ASC'){
        return $this->select(function(\Zend\Db\Sql\Select $select)use($order){
            $select->order($order);
        });
    }
    
    /**
     * Récupération
     * 
     * @param int $id
     * @param mixed $default
     * @return XObject
     */
    public function get($id, $default = null){
        $obj = $this->select(array('id' => $id))->current();
        if(!$obj){
            return $default;
        }
        return $obj;
    }
    
    /**
     * Vérification
     * 
     * @param int $id
     * @return boolean
     */
    public function verify($id){
        if(is_null($this->get($id))){
            return false;
        }
        return true;
    }
    
    /**
     * Suppression
     * 
     * @param int $id
     * @return int
     */
    public function erase($id){
        return $this->delete(array('id' => $id));
    }
    
    /**
     * Enregistrement
     * 
     * @param \Ticket\Model\Object\XObject $object
     * @return int
     */
    public function save(XObject $object){
        $data = $object->getArrayCopy(false);
        $id = $object->id;
        if($id == 0){
            $this->insert($data);
            return $this->getLastInsertValue();
        }elseif($this->verify($id)){
            $this->update($data, array('id' => $id));
            return $id;
        }else{
            return 0;
        }
    }
    
    /**
     * Exécution de requête Brute
     * 
     * @param string $sql
     * @return ResultSet
     */
    public function executeQuery($sql){
        return $this->getAdapter()
                ->query($sql)
                ->execute();
    }
}
